//
//  ViewController.m
//  自定义下载进度条
//
//  Created by Jorn.Wu on 15/5/15.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"
#import "ProgressView.h"

@interface ViewController ()
@property (nonatomic, strong) CALayer *layer;//单独创建出的layer
@property (nonatomic, strong) ProgressView *progressVeiw;//参数
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, weak) UILabel *label;

@end

@implementation ViewController

- (UILabel *)label{
    if (_label == nil) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 110, 300, 10)];
        _label = label;
        [self.view addSubview:_label];
    }
    return _label;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    //创建一个容器view
//    UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 3)];
//    containerView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:containerView];
    
//    //通过view的layer修改该容器view的相关属性
//    containerView.layer.frame = CGRectMake(0, 0, 200, 200);
//    containerView.layer.backgroundColor = [UIColor greenColor].CGColor;
//
//    自定义layer
//    self.layer = [CALayer layer];
//    self.layer.frame = containerView.bounds;
//    self.layer.backgroundColor = [UIColor greenColor].CGColor;
    
//    设置动画(必须单独创建出来)
//    self.layer = [CALayer layer];
//    self.layer.frame = CGRectMake(0, 0, 0, 3);
//    self.layer.backgroundColor = [UIColor greenColor].CGColor;
//    [containerView.layer addSublayer:self.layer];
//    [self performSelector:@selector(layerAnimation)
//               withObject:nil
//               afterDelay:3];//延迟三秒中后，自动触发layer的隐式动画
    
    
    
    self.progressVeiw = [[ProgressView alloc]initWithFrame:CGRectMake(20, 100, 300, 4)];
    self.progressVeiw.layer.borderWidth = 0.5;
    self.progressVeiw.layer.borderColor = [UIColor greenColor].CGColor;
    [self.view addSubview:self.progressVeiw];
    
    [self performSelector:@selector(layerAnimation)
               withObject:nil
               afterDelay:3.f];
//    创建定时器 ，每秒执行一次
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(layerAnimation)
                                                userInfo:nil
                                                 repeats:YES];
}
- (void)layerAnimation{
//    self.layer.frame = CGRectMake(0, 0, 100, 3);
//    self.layer.backgroundColor = [UIColor blackColor].CGColor;
//    self.progressVeiw.progress = 0.8;
    self.progressVeiw.progress = arc4random() % 100 / 100.f;//通过随机数或去progress值
    self.label.textColor = [UIColor blueColor];
    self.label.text = [NSString stringWithFormat:@"现在已完成：%0.2lf %%",self.progressVeiw.progress * 100];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
